<template>
	<view>
		<view style="background-color: #FFFFFF;">
			<cu-custom :isBack="true">
				<block slot="backText"></block>
				<block slot="content">{{corpCode}}-库房转移</block>
			</cu-custom>
		</view>
		<view class="cu-bar bg-white search fixed indexes" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input @confirm="sub" v-model="val" type="text" placeholder="输入关键词" focus></input>
			</view>
			<view class="action" style="float: right;">
				<button class="cu-btn bg-gradual-green shadow-blur round" @tap="showModal" data-target="DrawerModalR">推荐货位</button>
			</view>
			<view class="action" @click="toScan">
				<text class="cuIcon-scan" style="font-size: 56rpx;"></text>
			</view>
		</view>
		<view style="margin-top: 13%;">
			<!-- <view class="flex-sub text-center solid-bottom text-xl padding">
				<text class="text-black text-bold">调拨信息</text>
			</view>
		</view>
		<view class="text-gray cu-form-group " >
			<text class="text-blue">调拨单号：{{ctvcode}}</text>
		</view>
		<view > -->
			<view class="flex-sub text-center solid-bottom text-xl padding">
				<text class="text-black text-bold">货位信息</text>
			</view>
		</view>
		<view class="text-gray cu-form-group " style="color: red;">
			<text>货位：{{hwname}}</text>
		</view>
		<view class="text-gray cu-form-group " style="color: red;">
			<text>仓库：{{cwhname}}</text>
		</view>
		<view>
			<view class="flex-sub text-center solid-bottom text-xl padding">
				<text class="text-black text-bold">产品信息</text>
			</view>
		</view>
		<view class="flex-sub solid-bottom text-xl" >
			<view class="text-gray cu-form-group">
				<text class="text-blue margin-right-xs">存货编码：{{viewCode}}</text> 
			</view >
			<view class="text-gray cu-form-group">
				<text class="text-blue margin-right-xs">存货名称：{{cinvname}}</text> 
			</view>
			<view class="text-gray cu-form-group">
				<text class="margin-right-xs" style="color: red;font-weight: 1000;">上架数量：</text>
				<input placeholder="0" name="input" type="number" v-model="qty"></input>
			</view>
		</view>
		<view class="action-section">
			<button type="primary" class="no-border confirm-btn" :disabled="ishow" @click="uploadData">提交</button>
		</view>
		<view class="cu-modal drawer-modal justify-end" :class="modalName=='DrawerModalR'?'show':''" @tap="hideModal">
			<view class="cu-dialog basis-lg" @tap.stop="" :style="[{top:CustomBar+'px',height:'calc(100vh - ' + CustomBar + 'px)',width:'80%'}]">
				<view>
					<view v-if="cHList.length<1" class="nodata">暂无数据</view>
					<view class="cu-list menu"  v-for="(item,index) in cHList" :key="index">
						<view class="cu-item" >
							<view class="content padding-tb-sm">
								
								<view class="text-black text-sm view-s"  style="text-align: left;">仓库：<text  class="t-text"  style="float: right;"> {{item.cwhname}}</text></view>
								<view class="text-black text-sm view-s"  style="text-align: left;">货位：<text  class="t-text" > {{item.cposname}}</text></view>
								<view class="text-black text-sm view-s"  style="text-align: left;">数量：<text  class="t-text" > {{item.iquanttity}}</text></view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				CustomBar: this.CustomBar, //滚动定位用
				val:'',//用于接收扫码的值
				hwcode:'',//货位编码
				hwname:"",//货位名称
				qty:'',//调整数量
				ishow:false,//是否可用
				viewCode:'',//存货
				cinvcode:'',//存货
				cinvname:'',//存货
				id:'',//调拨单编码
				cwhcode:'',//调拨后仓库
				cwhname:'',
				ctvcode:'',//调拨单code
				modalName:null,
				cHList:[],
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'auditlist', 'corpCode']),
		},
		onLoad(data) {
			this.id = data.id
			this.val = '';
			// this.sub();
		},
		methods: {
			/** 扫发出声音*/
			makeSound(name){
				console.log("=====testClick=====");
				let src = '../../static/'+name+'.mp3';
				//实例化声音  
				const Audio = uni.createInnerAudioContext();
				Audio.autoplay = true;
				Audio.src = src; //音频地址  
				Audio.play(); //执行播放  
				Audio.onError((res) => {
				});
				Audio.onPause(function() {
					console.log('end');
					Audio.destroy();
				});
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
				this.findHWData();
			},
			hideModal(e) {
				this.modalName = null
			},
			findHWData(){
				let cinvcode=this.viewCode;
				if(cinvcode == undefined || cinvcode == ''){
					return;
				}
				uni.request({
					url:this.serverURL_a+'/wh/u8/u8InvpositionSum/sumListData?__ajax=json',
					data:{
						cinvcode : cinvcode,
						"orderBy":"cbatch desc",
						__sid:this.userInfo.sessionid
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(data){
						let ret = data.data;
						if(data.statusCode!="200"){
							uni.showModal({
								content: ret.message,
								showCancel:false,
							});
							return;
						}
						this.cHList=data.data;
					}.bind(this),
					fail:function(){
						this.$api.msg('服务已停止,请稍后再试');
						return;
					}.bind(this),
				});
			},
			loadData(){
				
			},
			/**
			 * 使用于扫码事件发生
			 */
			sub(){
				/* if(this.val.indexOf("||st12")>-1){
						uni.request({
							url:this.serverURL_a+'/wh/db/whDbTransVouch/findDbByCtvcode',
							data:{
								cbarcode : this.val,
								__sid:this.userInfo.sessionid
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
							},
							method: 'POST',
							success:function(data){	
								this.val = '';
								var res = data.data;
								if(res.result == 'true'){
									var resDt = res.data;
									//封装主表数据
									this.id=resDt.id;
									this.ctvcode = resDt.ctvcode;
									this.cwhcode=resDt.ciwhcode;
									this.$api.msg('扫描成功');
								}else{
									uni.showModal({
										content: res.message,
										showCancel:false,
									});
									return;
						}}.bind(this),
							fail:function(){
								this.$api.msg('服务已停止,请稍后再试');
								return;
							}.bind(this),
						});
						return;
				}
				if(this.id==null||this.id==""){
					uni.showModal({
					    content: "请先扫描调拨单！",
						showCancel:false,
					});
					return;
				} */
				if(this.val.indexOf("hw") > -1){
					uni.request({
						url:this.serverURL_a+'/so/App/getHw',
						data:{
							hw : this.val,
							__sid:this.userInfo.sessionid
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						},
						method: 'POST',
						success:function(data){
							this.val = '';
							var requrt = data.data;
							if(requrt.code == '1001'){
								var hwdata = requrt.resMap;
								this.hwcode = hwdata.cposcode;
								this.hwname = hwdata.cposname;
								this.cwhcode = hwdata.basWarehouse.viewCode;
								this.cwhname = hwdata.basWarehouse.cwhname;
							}else{
								uni.showModal({
								    content: requrt.msg,
									showCancel:false,
								});
								return;
							}
						}.bind(this),
						fail:function(){
							this.$api.msg('服务已停止,请稍后再试');
							return;
						}.bind(this),
					});
					return;
				// }else if(this.val == '' || (this.val.indexOf("sc") == -1 && this.val.indexOf("dh") == -1 && this.val.indexOf("po") == -1 && this.val.indexOf("611-") == -1 && this.val.indexOf("inv") == -1)){
				}else if(this.val == ''){
					this.val = '';
					uni.showModal({
					    content: "请扫描正确的条码!",
						showCancel:false,
					});
					this.val = '';
					return;
				}
				uni.request({
					// url:this.serverURL_a+'/bas/basInventory/findMapCinvcode',
					url:this.serverURL_a+'/barcode/decode/getInvInfo',
					data:{
						// hw : this.val,
						__sid:this.userInfo.sessionid,
						barCode: this.val,
						bneedStore: '0',
						bmustAcct:'0'
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
					},
					method: 'POST',
					success:function(res){
						console.log(res,'res=========');
						this.val = '';
						if(res.data.result=="false"){
							uni.showModal({
							  content: res.data.message,
							  showCancel:false,
							})
							return false;
						}
						if(this.viewCode == res.data.data.basInv.viewCode){
							this.qty += 1;
						}else{
							this.viewCode = res.data.data.basInv.viewCode;//存货
							this.cinvcode = res.data.data.basInv.cinvcode;//存货
							this.cinvname = res.data.data.basInv.cinvname;//存货 
							this.qty = 1;
						}
						// this.viewCode = res.data.bPos.viewCode;//存货
						// this.cinvcode = res.data.bPos.cinvcode;//存货
						// this.cinvname = res.data.bPos.cinvname;//存货 
						// this.qty = 1;
					}.bind(this),
					fail: function() {
						uni.hideLoading()
						this.$api.msg('无法连接服务，请联系管理');
						this.isReC = false;
						return;
					}.bind(this)
				})
			},
			/**
			 * 扫码
			 */
			toScan(){
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						var rdata = res.result;
						this.val = rdata;
						setTimeout(function(){
							this.sub();
						}.bind(this),100);
						
					}.bind(this)
				});
			},
			/**
			 * 数据上传
			 */
			uploadData(){
				this.ishow = true;
				if(this.id== ''){
					this.$api.msg("请扫描调拨单!");
					this.ishow = false;
					this.makeSound("sb");
					return;
				}
				if(this.cinvcode == ''|| this.hwcode == ''){
					this.$api.msg("请检查数据，存货和货位信息不能为空!");
					this.ishow = false;
					this.makeSound("sb");
					return;
				}
				if(this.qty <= 0 ){
					this.$api.msg("请检查数据，调整数量等于和小于【零】，无意义!");
					this.ishow = false;
					this.makeSound("sb");
					return;
				}
				
				//提交数据
				uni.showModal({
					content: '是否提交\r\n当前调整数量：'+this.qty,
					success: function(res) {
						if(res.confirm){
							uni.showLoading({
							    title: '数据提交中',
								 mask:true
							});
							uni.request({
								url:this.serverURL_a + '/wh/App/dbJustInv?__ajax=json',
								data:{
									// id:this.id,
									cposcode:this.hwcode,
									cinvcode:this.cinvcode,
									iquantity:this.qty,
									cwhcode:this.cwhcode,
									__sid:this.userInfo.sessionid
								},
								header: {
									'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
								},
								method: 'POST',
								success:function(data){
									uni.hideLoading();
									var request = data.data;
									this.ishow = false;
									if(data.statusCode!="200"){
										uni.showModal({
										    content: request.message,
											showCancel:false,
										});
										this.makeSound("sb");
										return;
									}
									if(request.result == 'true'){
										this.$api.msg('提交成功!');
										this.viewCode = '';//存货
										this.cinvcode = '';//存货
										this.cinvname = '';//存货 
										this.qty = '';
										this.makeSound("cg");
										return;
									}else{
										uni.showModal({
											content: request.message,
											showCancel:false,
											success: function(res) {
												return
											},
										});
										this.makeSound("sb");
										return;
									}
								}.bind(this),
								fail:function() {
									this.$api.msg('无法连接服务，请联系管理');
									return;
								}.bind(this)
							})
						}else if (res.cancel) {
							return
						}
					}.bind(this)
				});
			},
			
		}
	}
</script>

<style>
/* 底部栏 */
	.action-section{
		/* #ifdef H5 */
		/* #endif */
		position:fixed;
		left: 75%;
		bottom:30upx;
		z-index: 95;
		display: flex;
		align-items: center;
		background: rgba(255,255,255,.9);
		border-radius: 40upx;
		.checkbox{
			height:52upx;
			position:relative;
			image{
				width: 52upx;
				height: 100%;
				position:relative;
				z-index: 5;
			}
		}
		.clear-btn{
			position:absolute;
			left: 26upx;
			top: 0;
			z-index: 4;
			width: 0;
			height: 52upx;
			line-height: 52upx;
			padding-left: 38upx;
			font-size: $font-base;
			color: #fff;
			background: $font-color-disabled;
			border-radius:0 50px 50px 0;
			opacity: 0;
			transition: .2s;
			&.show{
				opacity: 1;
				width: 120upx;
			}
		}
		.total-box{
			flex: 1;
			display:flex;
			flex-direction: column;
			text-align:right;
			padding-right: 40upx;
			.price{
				font-size: $font-lg;
				color: $font-color-dark;
			}
			.coupon{
				font-size: $font-sm;
				color: $font-color-light;
				text{
					color: $font-color-dark;
				}
			}
		}
		.confirm-btn{
			padding: 0 38upx;
			margin: 0;
			border-radius: 100px;
			height: 76upx;
			line-height: 76upx;
			font-size: $font-base + 2upx;
			background: $uni-color-primary;
		}
	}
</style>
